package com.wzcl.app.controller;


import com.wzcl.app.dao.model.Topic;
import com.wzcl.app.dao.model.TopicInfo;
import com.wzcl.app.dao.model.TopicOption;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.TopicDto;
import com.wzcl.app.model.dto.TopicInfoDto;
import com.wzcl.app.model.dto.TopicOptionDto;
import com.wzcl.app.model.vo.TopicInfoVo;
import com.wzcl.app.model.vo.TopicOptionVo;
import com.wzcl.app.model.vo.TopicVo;
import com.wzcl.app.service.TopicInfoService;
import com.wzcl.app.service.TopicOptionService;
import com.wzcl.app.service.TopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/topic")
@Api(value = "题库管理controller", tags = {"题库管理相关接口"})
public class TopicController {

    @Resource
    private TopicService service;

    @Resource
    private TopicInfoService topicInfoService;

    @Resource
    private TopicOptionService topicOptionService;

    @ApiOperation("创建题目大纲")
    @PostMapping("/add")
    public RVO<TopicVo> addTopic(@RequestBody @Valid TopicDto topicDto){
        Topic topic = service.addTopic(topicDto);
        return RVO.success(TopicVo.create(topic));
    }

    @ApiOperation("创建问题")
    @PostMapping("/addInfo")
    public RVO<TopicInfoVo> addTopicInfo(@RequestBody @Valid TopicInfoDto topicInfoDto){
        TopicInfo topicInfo = topicInfoService.addTopicInfo(topicInfoDto);
        return RVO.success(TopicInfoVo.create(topicInfo));
    }

    @ApiOperation("创建问题选项")
    @PostMapping("/addOption")
    public RVO<TopicOptionVo> addOption(@RequestBody @Valid TopicOptionDto topicInfoDto){
        TopicOption topicOption = topicOptionService.addTopicOption(topicInfoDto);
        return RVO.success(TopicOptionVo.create(topicOption));
    }

    @ApiOperation("修改题目大纲基本信息")
    @PostMapping("/updateBasic")
    public RVO<String> updateBasicTopic(@RequestBody TopicDto topicDto){
        service.updateBasicTopic(topicDto);
        return RVO.success();
    }
//
//    @ApiOperation("修改题目大纲知识点")
//    @PostMapping("/getKnowledgePoint")
//    public RVO<List<KnowledgePointVo>> getKnowledgePoint(@RequestBody TopicDto topicDto){
//        service.getKnowledgePoint(topicDto.getId());
//
//        return RVO.success();
//    }

    @ApiOperation("修改题目大纲知识点")
    @PostMapping("/updateKnowledgePoint")
    public RVO<String> updateKnowledgePointTopic(@RequestBody TopicDto topicDto){
        service.updateTopicKnowledgePoint(topicDto);
        return RVO.success();
    }

    @ApiOperation("修改问题")
    @PostMapping("/updateInfo")
    public RVO<TopicInfoVo> updateTopicInfo(@RequestBody @Valid TopicInfoDto topicInfoDto){
        TopicInfo topicInfo = topicInfoService.updateTopicInfo(topicInfoDto);
        return RVO.success(TopicInfoVo.create(topicInfo));
    }

    @ApiOperation("修改问题选项")
    @PostMapping("/updateOption")
    public RVO<TopicOptionVo> updateOption(@RequestBody @Valid TopicOptionDto topicInfoDto){
        TopicOption topicOption = topicOptionService.updateTopicOption(topicInfoDto);
        return RVO.success(TopicOptionVo.create(topicOption));
    }

    @ApiOperation("删除题目大纲")
    @PostMapping("/del")
    public RVO<TopicVo> delTopic(@RequestBody TopicDto topicDto){
        Topic topic = service.delTopicById(topicDto.getId());
        return RVO.success(TopicVo.create(topic));
    }

    @ApiOperation("删除问题")
    @PostMapping("/delInfo")
    public RVO<TopicInfoVo> delTopicInfo(@RequestBody TopicInfoDto topicInfoDto){
        TopicInfo topicInfo = topicInfoService.delTopicInfoById(topicInfoDto.getId());
        return RVO.success(TopicInfoVo.create(topicInfo));
    }

    @ApiOperation("删除问题选项")
    @PostMapping("/delOption")
    public RVO<TopicOptionVo> delOption(@RequestBody TopicOptionDto topicInfoDto){
        TopicOption topicOption = topicOptionService.delTopicOptionById(topicInfoDto.getId());
        return RVO.success(TopicOptionVo.create(topicOption));
    }

    @ApiOperation("获取题目大纲")
    @PostMapping("/get")
    public RVO<TopicVo> getTopic(@RequestBody TopicDto topicDto){
        Topic topic = service.getTopicById(topicDto.getId());
        if(topic != null) {
            return RVO.success(TopicVo.create(topic));
        }
        return RVO.success(null);
    }

    @ApiOperation("获取问题")
    @PostMapping("/getInfo")
    public RVO<TopicInfoVo> getTopicInfo(@RequestBody TopicInfoDto topicInfoDto){
        TopicInfo topicInfo = topicInfoService.getTopicInfoById(topicInfoDto.getId());
        if (topicInfo != null) {
            return RVO.success(TopicInfoVo.create(topicInfo));
        }
        return RVO.success(null);
    }

    @ApiOperation("获取问题选项")
    @PostMapping("/getOption")
    public RVO<TopicOptionVo> getOption(@RequestBody TopicOptionDto topicInfoDto){
        TopicOption topicOption = topicOptionService.getTopicOptionById(topicInfoDto.getId());
        if (topicOption != null) {
            return RVO.success(TopicOptionVo.create(topicOption));
        }
        return RVO.success(null);
    }

//    @ApiOperation("获取题目大纲List")
//    @PostMapping("/getList")
//    public RVO<List<TopicVo>> getTopicList(@RequestBody TopicDto topicDto){
//        List<Topic> list = service.getTopicList(topicDto);
//        if(list != null && list.size() > 0) {
//            return RVO.success(list.stream().map(TopicVo::create).collect(Collectors.toList()));
//        }
//        return RVO.success(null);
//    }
//
//    @ApiOperation("获取问题List")
//    @PostMapping("/getInfoList")
//    public RVO<List<TopicInfoVo>> getTopicInfoList(@RequestBody TopicInfoDto topicInfoDto){
//        List<TopicInfo> list = topicInfoService.getTopicInfoList(topicInfoDto);
//        if(list != null && list.size() > 0) {
//            return RVO.success(list.stream().map(TopicInfoVo::create).collect(Collectors.toList()));
//        }
//        return RVO.success(null);
//    }
//
//    @ApiOperation("获取问题选项List")
//    @PostMapping("/getOptionList")
//    public RVO<List<TopicOptionVo>> getOptionList(@RequestBody TopicOptionDto topicInfoDto){
//        List<TopicOption> list = topicOptionService.getTopicOptionList(topicInfoDto);
//        if(list != null && list.size() > 0) {
//            return RVO.success(list.stream().map(TopicOptionVo::create).collect(Collectors.toList()));
//        }
//        return RVO.success(null);
//    }

    @ApiOperation("获取题目大纲Page")
    @PostMapping("/getPage")
    public RVO<List<TopicVo>> getTopicPage(@RequestBody TopicDto topicDto){
        MyPage<Topic> page = service.getTopicPage(topicDto);
        if(page != null && page.getContent().size() > 0) {
            return RVO.success(page.getContent().stream().map(TopicVo::create).collect(Collectors.toList()),page);
        }
        return RVO.success(null);
    }

    @ApiOperation("获取问题Page")
    @PostMapping("/getInfoPage")
    public RVO<List<TopicInfoVo>> getTopicInfoPage(@RequestBody TopicInfoDto topicInfoDto){
        MyPage<TopicInfo> page = topicInfoService.getTopicInfoPage(topicInfoDto);
        if(page != null && page.getContent().size() > 0) {
            return RVO.success(page.getContent().stream().map(TopicInfoVo::create).collect(Collectors.toList()),page);
        }
        return RVO.success(null);
    }

    @ApiOperation("获取问题选项Page")
    @PostMapping("/getOptionPage")
    public RVO<List<TopicOptionVo>> getOptionPage(@RequestBody TopicOptionDto topicInfoDto){
        MyPage<TopicOption> page = topicOptionService.getTopicOptionPage(topicInfoDto);
        if(page != null && page.getContent().size() > 0) {
            return RVO.success(page.getContent().stream().map(TopicOptionVo::create).collect(Collectors.toList()),page);
        }
        return RVO.success(null);
    }

    @ApiOperation("随机选题")
    @PostMapping("/getRandom")
    public RVO<List<TopicVo>> getOptionPage(@RequestBody TopicDto topicDto){
        List<Topic> list = service.getTopicRandList(topicDto);
        if(!list.isEmpty()) {
            return RVO.success(list.stream().map(TopicVo::create).collect(Collectors.toList()));
        }
        return RVO.success(null);
    }
}
